home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / letz / let.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  4.0 KB  |  182 lines

  1. #include "all.h"
  2. #include <math.h>
  3. #include <time.h>
  4. extern int ngerror;
  5. extern int in_recover;
  6. extern int curx;
  7. extern int cury;
  8. extern int max_x,max_y;
  9. #define tok(j) (tk[j])
  10. #define dbg if (gle_debug>0)
  11. extern char file_name[];
  12. extern int iserr;
  13. int gle_debug,changed;
  14. extern int trace_on;
  15. extern int exit_manip;
  16. int isating;
  17. int islogging;
  18. int load_list;
  19. int dpoints;
  20. int outwidth1=8,outwidth2=8;
  21. int getrange(char *s, double *min, double *max, double *step);
  22. char *between=" ";
  23. char strmiss[40];
  24. int stripspace(char *s);
  25. int getstr(char *s);
  26. FILE *ip;
  27. /*---------------------------------------------------------------------------*/
  28. main(int argc, char *argv[])
  29. {
  30.     static char inbuff[200];
  31.     static char *tk[500];
  32.     static char tkbuff[500];
  33.     char dfile[80];
  34.     char bfile[80];
  35.     char lfile[80];
  36.     char xstr[90];
  37.  
  38.     char outstr[80];
  39.     long pcode[200];
  40.     int plen;
  41.     int cp,pii;
  42.     int vtype = 1;
  43.     int i,al,ntok,f,nx,ny,xi,yi;
  44.     FILE *fp;
  45.     char space_str[] = " ";
  46.  
  47.     double xmin,xmax,xstep,v;
  48.     double x,y,ymin,ymax,ystep;
  49.     char exp[200];
  50.  
  51.     ngerror = 0;
  52.     if (argc>1) {
  53.         strcpy(lfile,argv[1]);
  54.         if (strchr(lfile,'.')==NULL) strcat(lfile,".let");
  55.         ip = fopen(lfile,"r");
  56.         if (ip==NULL) {
  57.             printf("Unable to open let file {%s} \n",lfile);
  58.             exit(1);
  59.         }
  60.     } else ip = stdin;
  61.  
  62. indfile:
  63.     printf("File to store data in ? "); getstr(dfile);
  64.     if (strlen(dfile)==0) goto indfile;
  65.     if (strchr(dfile,'.')!=NULL) *strchr(dfile,'.') = 0;
  66.     strcpy(bfile,dfile);
  67.     strcat(bfile,".let");
  68.     strcat(dfile,".z");
  69.     printf("Storing data in {%s} and commands in {%s} \n",dfile,bfile);
  70.  
  71.     printf("Enter xmin,xmax,xstep (on one line) [0,30,1] ? "); getstr(xstr);
  72.     getrange(xstr,&xmin,&xmax,&xstep);
  73.  
  74.     printf("Enter ymin,ymax,ystep [0,30,1] ? "); getstr(xstr);
  75.     getrange(xstr,&ymin,&ymax,&ystep);
  76.  
  77.     printf("\nFor y = %g to %g step %g\n",ymin,ymax,ystep);
  78.     printf("For x = %g to %g step %g\n",xmin,xmax,xstep);
  79.  
  80.     printf("Valid funtions:  (trig functions use radians)\n");
  81.     printf("    abs(), atn(), cos(), exp(), fix(),\n");
  82.     printf("    int(), log(), log10(), rnd(), sgn(), sin(),\n");
  83.     printf("    sqr(), sqrt(), tan(), PI, (x^3 = x*x*x) \n");
  84.     printf("    e.g.    3*x^2+sin(y*180/pi)\n");
  85.  
  86.     var_findadd("X",&pii,&vtype);
  87.     var_findadd("Y",&pii,&vtype);
  88.     var_findadd("PI",&pii,&vtype);
  89.     var_set(pii,3.141592654);
  90.     token_space();
  91.  
  92. tryagain:;
  93.     ngerror = 0;
  94.     printf("Enter equation:   \n ? "); getstr(exp);
  95.     vtype = 1; plen = 0;
  96.     stripspace(exp);
  97.     printf("z = %s \n",exp);
  98.     polish(exp,(char *) pcode,&plen,&vtype);
  99.     if (ngerror!=0) goto tryagain;
  100.  
  101.     fp = fopen(dfile,"w");
  102.     if (fp==NULL) {
  103.         printf("Unable to open {%s} \n",dfile);
  104.         exit(1);
  105.     }
  106.  
  107.     printf("\n");
  108.     nx = (xmax-xmin)/xstep + 1;
  109.     ny = (ymax-ymin)/ystep + 1;
  110.     fprintf(fp,"! nx %d ny %d xmin %g xmax %g ymin %g ymax %g \n",nx,ny,xmin,xmax,ymin,ymax);
  111.  
  112.     printf("Y = ");
  113.     for (y=ymin, yi=0; yi<ny ; yi++,y+=ystep) {
  114.       printf("%g ",y);
  115.       for (x=xmin, xi=0; xi<nx; xi++, x+=xstep) {
  116.         eval_setxy(x,y);
  117.         cp = 0;
  118.         eval(pcode,&cp,&v,outstr,&vtype);
  119.         fprintf(fp,"%g ",v);
  120.       }
  121.       fprintf(fp,"\n");
  122.     }
  123.     fclose(fp);
  124.     fclose(ip);
  125.     printf("\n");
  126.  
  127.     fp = fopen(bfile,"w");
  128.     if (fp==NULL) {
  129.         printf("Unable to open cmd file {%s} \n",bfile);
  130.         exit(1);
  131.     }
  132.     fprintf(fp,"%s\n",dfile);
  133.     fprintf(fp,"%g, %g, %g\n",xmin,xmax,xstep);
  134.     fprintf(fp,"%g, %g, %g\n",ymin,ymax,ystep);
  135.     fprintf(fp,"%s\n",exp);
  136.     fclose(fp);
  137.     printf("Use   LETZ %s   to remake the same data file\n",bfile);
  138. }
  139. getstr(char *s)
  140. {
  141.     char *ss;
  142.     ss = fgets(s,200,ip);
  143.     if (ss==NULL) {
  144.         printf("\nERROR, End of input file \n");
  145.         exit(1);
  146.     }
  147.     ss = strchr(s,'\n');
  148.     if (ss!=NULL) *ss = 0;
  149.     ss = strchr(s,'!');
  150.     if (ss!=NULL) *ss = 0;
  151. }
  152. getrange(char *s, double *min, double *max, double *step)
  153. {
  154.     *min = 0; *max = 30; *step = 1;
  155.     s = strtok(s,", :\n\t");
  156.     if (s!=NULL) *min = atof(s);
  157.     s = strtok(NULL,", :\n\t");
  158.     if (s!=NULL) *max = atof(s);
  159.     *step = (*max - *min) / 30.0;
  160.     s = strtok(NULL,", :\n\t");
  161.     if (s!=NULL) *step = atof(s);
  162. }
  163. gle_abort(char *s)
  164. {
  165.     printf("Aborting %s\n",s);
  166.     exit(1);
  167. }
  168. stripspace(char *ss)
  169. {
  170.     char buff[300],*b,*s=ss;
  171.     b = buff;
  172.     for (; *s!=0; s++)
  173.          if (*s!=' ' && *s!='\t') *b++ = *s;
  174.     *b++ = 0;
  175.     strcpy(ss,buff);
  176. }
  177. tt_inkey()
  178. {}
  179. #ifdef unix
  180. getch(){}
  181. #endif
  182.